-- NewsPaperView

local NewsPaperRender11 = import(".renders.NewsPaperRender11")
local NewsPaperRender12 = import(".renders.NewsPaperRender12")
local NewsPaperRender13 = import(".renders.NewsPaperRender13")
local NewsPaperRender21 = import(".renders.NewsPaperRender21")
local NewsPaperRender22 = import(".renders.NewsPaperRender22")
local NewsPaperRender31 = import(".renders.NewsPaperRender31")
local NewsPaperRender33 = import(".renders.NewsPaperRender33")
local NewsPaperRender34 = import(".renders.NewsPaperRender34")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

local RenderByType = {
    [11] = NewsPaperRender11,
    [12] = NewsPaperRender12,
    [13] = NewsPaperRender13,
    [21] = NewsPaperRender21,
    [22] = NewsPaperRender22,
    [31] = NewsPaperRender31,
    [33] = NewsPaperRender33,
    [34] = NewsPaperRender34,
}
local arrWeek = {L("champion_weekday1"), L("champion_weekday2"), L("champion_weekday3"), L("champion_weekday4"), L("champion_weekday5"), L("champion_weekday6"), L("champion_weekday7")}

ClassRef.VIEW_ROOT_RES = Res.NewsPaperScene

function ClassRef:init()
    self._ui.Button_close:addClickEventListener(function()
        executeFunc(self._onCloseView)
    end)
    -- 翻转按钮
    self._ui.btn_page:setVisible(false)
    -- 二维码
    self._ui.img_ewm:setVisible(false)
    
    self.pctl = app:getInst("PhotoController")
    self:initButtons()

    self._allRenders = {}
    for i = 1, 16 do
        local render = self._ui["Panel/Panel_node/Node_"..i]
        if render then
            render:setVisible(false)
            self._allRenders[i] = render
        end
    end
end

function ClassRef:refreshNewsPaper()
    if self._data then
        local data = self._data
        local dateStr = string.gsub(data.date, "%/", "%.")
        self._ui.date:setString(dateStr)
        local dateSplit = string.split(data.date, "/")
        local weekNum = TimeUtil.getWeekByYearMonthDay(dateSplit[1], dateSplit[2], dateSplit[3])
        self._ui.week:setString(arrWeek[weekNum])
        self._ui.No:setString(string.format(L("NewsPaperIssueTips"), data.issue))
        local newsInfo = data.info
        for i, v in ipairs(newsInfo) do
            local newsRender = RenderByType[v.formatType].create()            
            newsRender:setData(v)
            newsRender:onOperate(self._onOperate)
            if self._allRenders[v.pos] then
                self._allRenders[v.pos]:addChild(newsRender)
                self._allRenders[v.pos]:setVisible(true)
            end
        end
    end
end

function ClassRef:initButtons()
    local shareConfig = {}
    cc.SpriteFrameCache:getInstance():addSpriteFrames('ui_resources/main1.plist')
    if app:getPublisher() == Enums.Publisher.ema then
        shareConfig = {
            {imagePath = Res.ShareIcons.FaceBook, callback = function()
                app.channelAPI:share(nil, Enums.ShareType.facebook, L("fb_share_title"), L("fb_share_desc"), self.pctl:getSnapShotPath())
            end},
            {imagePath = Res.ShareIcons.Twitters, callback = function()
                app.channelAPI:share(nil, Enums.ShareType.twitter, L("twt_share_title"), L("twt_share_desc"), self.pctl:getSnapShotPath())
            end},
        }
    elseif app:getPublisher() == Enums.Publisher.tencent then
        local wgPlatform = app.channelAPI:getWGPlatform()
        if wgPlatform == Enums.MSDK_PLATFORM.QQ  then
            shareConfig = {
                {imagePath = Res.ShareIcons.QQ, callback = function()
                    
                    if not app.channelAPI:isPlatformInstalled(Enums.MSDK_PLATFORM.QQ) then
                        display.pushToast(L("QQNotInstalledForShare"))
                        return
                    end
                    
                    app.channelAPI:share(nil, Enums.ShareType.qqSession, L("title"), L("desc"), self.pctl:getSnapShotPath())
                end},
                {imagePath = Res.ShareIcons.QQZone, callback = function()
                    
                    if not app.channelAPI:isPlatformInstalled(Enums.MSDK_PLATFORM.QQ) then
                        display.pushToast(L("QQNotInstalledForShare"))
                        return
                    end
                
                    app.channelAPI:share(nil, Enums.ShareType.qqZone, L("title"), L("desc"), self.pctl:getSnapShotPath())
                end},
            }
        elseif wgPlatform == Enums.MSDK_PLATFORM.WX then
            shareConfig = {
                {imagePath = Res.ShareIcons.WX, callback = function()
                   
                    if not app.channelAPI:isPlatformInstalled(Enums.MSDK_PLATFORM.WX) then
                        display.pushToast(L("WXNotInstalledForShare"))
                        return
                    end
                    
                    app.channelAPI:share(nil, Enums.ShareType.wxSession, L("title"), L("desc"), self.pctl:getSnapShotPath())
                end},
                {imagePath = Res.ShareIcons.WXZone, callback = function()
                
                    if not app.channelAPI:isPlatformInstalled(Enums.MSDK_PLATFORM.WX) then
                        display.pushToast(L("WXNotInstalledForShare"))
                        return
                    end
                
                    app.channelAPI:share(nil, Enums.ShareType.wxCircle, L("title"), L("desc"), self.pctl:getSnapShotPath())
                end},
            }
        end
    end

    for i = 1, 4 do
        local btn = self._ui["Panel/btn_"..i]
        if btn then
            local cf = shareConfig[i]
            if cf then
                btn:loadTextures(cf.imagePath, cf.imagePath, cf.imagePath, 1)
                btn:addClickEventListener(function(sender)
                    self:setBtnState(false)                    
                    self.pctl:snapshotNode(self, cf.callback)
                    scheduler.newCron(function()
                        if self and self._ui and cc.isAlive(self._ui.Button_close) then
                            self._ui.Button_close:setVisible(true)
                        end
                    end, 1)
                end)
            else
                btn:setVisible(false)
            end
        end
    end
end

function ClassRef:setBtnState(b)
    self._ui.Button_close:setVisible(b)
    self._ui.btn_1:setVisible(b)
    self._ui.btn_2:setVisible(b)        
    self._ui.btn_3:setVisible(b)
    self._ui.btn_4:setVisible(b)
end

return ClassRef